<template >
  <div>
    <keep-alive >
      <router-view v-if="$route.meta.keepAlive"/>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"/>
  </div> 
</template>
<script>
import Vue from 'vue'
import eventBus from '@/utils/helper/eventBus'
  
export default {
  name: 'RouteView',
  components: {
    eventBus
  },
  props: {
    keepAlive: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      isRouterAlive : true,
      keep_Alive: true
    }
  },
  created(){
    var _this = this
      eventBus.$on('reload',(message)=>{
          _this.reload ()
          
      })
  },
  
  watch:{
    $route(to,from){
      this.$nextTick(function(){
          this.$route.meta.keepAlive = true
      })
    }
  },
  methods:{
    reload (){
        this.$route.meta.keepAlive = false
       
        this.$nextTick(function(){
          this.$route.meta.keepAlive = true
           this.$route.params.name = ''
            this.$route.params.cuId = ''
              this.$route.params.subject2Id = ''
           
          const vnode = this.$vnode
          this.destroyCache(vnode)
       })
    },
    destroyCache(vnode){
      const parentVnode = vnode.parent 
      if (parentVnode && parentVnode.componentInstance.cache) {
        
        var key = vnode.key == null
              ? vnode.componentOptions.Ctor.cid + (vnode.componentOptions.tag ? `::${vnode.componentOptions.tag}` : '')
              : vnode.key
        
            var cache = parentVnode.componentInstance.cache
            var keys  = parentVnode.componentInstance.keys
            if (cache[key]) {
                  this.$destroy()
                  // remove key
                  if (keys.length) {
                    var index = keys.indexOf(key)
                    if (index > -1) {
                        keys.splice(index, 1)
                    }
                  }
                  cache[key] = null
            }
      }
    }
  }
}
</script>
